package com.jyxd.web.service.assessment.basic.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyxd.web.dao.basic.NursingTaskMapper;
import com.jyxd.web.data.basic.NursingTask;
import com.jyxd.web.data.dto.NursingTaskDTO;
import com.jyxd.web.data.patient.Patient;
import com.jyxd.web.mq.NursingTaskExecuteSender;
import com.jyxd.web.service.assessment.basic.NursingTaskService;
import com.jyxd.web.service.patient.PatientService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NursingTaskImpl extends ServiceImpl<NursingTaskMapper, NursingTask> implements NursingTaskService {

    private final Logger logger = LoggerFactory.getLogger(NursingTaskImpl.class);

    private final NursingTaskExecuteSender nursingTaskExecuteSender;
    private final PatientService patientService;

    public NursingTaskImpl(NursingTaskExecuteSender nursingTaskExecuteSender, PatientService patientService) {
        this.nursingTaskExecuteSender = nursingTaskExecuteSender;
        this.patientService = patientService;
    }

    @Override
    public List<NursingTask> queryByPatientId(String patientId) {
        return this.baseMapper.queryByPatientId(patientId);
    }

    @Override
    public long repairNursingTask(Patient patient, NursingTask nursingTask, Date startTime, Date endTime) {
        long startTimestamp = startTime.getTime();
        long currentTimestamp = endTime.getTime();

        if (patient.getStatus() == 1 && patient.getFlag() != 1) {//患者已经出科
            //优先使用出科时间
            Date stopTime = patient.getExitTime();
            if (ObjectUtils.isEmpty(stopTime)) {
                //获取死亡时间
                stopTime = patient.getDeathTime();
            }

            if (ObjectUtils.isNotEmpty(stopTime)) {
                //只补录出科之前得数据
                currentTimestamp = Math.min(stopTime.getTime(), currentTimestamp);
            }
        }
        //fix 自动生成护理单的时候生成了入科之前数据BUG
        if (patient.getEnterTime() != null) {
            long enterTimestamp = patient.getEnterTime().getTime();
            startTimestamp = Math.max(enterTimestamp, startTimestamp);
        }

        while (startTimestamp <= currentTimestamp) {
            Date oldRecordDateTime = new Date(startTimestamp);
            logger.info("补录数据:{},当前时间:{}", DateUtil.formatDateTime(oldRecordDateTime), DateUtil.formatDateTime(new Date()));
            NursingTaskDTO nursingTaskDTO = BeanUtil.copyProperties(nursingTask, NursingTaskDTO.class);
            nursingTaskDTO.setDataTime(oldRecordDateTime);
            logger.info("NursingTaskDTO({}):{}", nursingTaskDTO.hashCode(), nursingTaskDTO);
            nursingTaskExecuteSender.send(nursingTaskDTO);
            startTimestamp += nursingTask.getFrequencyDuration();
        }
        return startTimestamp;
    }
}
